<template>
  <div>
    <el-dialog :title="title" :visible.sync="visibleOfDialog">
      <el-form ref="form" :model="formData" size="mini" label-width="80px">
        <el-row>
          <el-col :span='12'>
            <el-form-item label="用户名">
              <el-input v-model="formData.user_name" placeholder="请输入用户名" clearable></el-input>
            </el-form-item>
          </el-col>
          <el-col :span='12'>
            <el-form-item label="昵称">
              <el-input v-model="formData.nick_name" placeholder="请输入昵称" clearable></el-input>
            </el-form-item>
          </el-col>
          <el-col :span='12'>
            <el-form-item label="姓名">
              <el-input v-model="formData.contact_name" placeholder="请输入姓名" clearable></el-input>
            </el-form-item>
          </el-col>
          <el-col :span='12'>
            <el-form-item label="密码">
              <el-input v-model="formData.password" placeholder="请输入密码" clearable show-password></el-input>
            </el-form-item>
          </el-col>
          <el-col :span='12'>
            <el-form-item label="密码盐">
              <el-input v-model="formData.salt" placeholder="请输入密码盐" clearable></el-input>
            </el-form-item>
          </el-col>
          <el-col :span='12'>
            <el-form-item label="头像">
              <el-input v-model="formData.avatar" placeholder="请输入头像" clearable></el-input>
            </el-form-item>
          </el-col>
          <el-col :span='12'>
            <el-form-item label="电子邮箱">
              <el-input v-model="formData.email" placeholder="请输入电子邮箱" clearable></el-input>
            </el-form-item>
          </el-col>
          <el-col :span='12'>
            <el-form-item label="手机号码">
              <el-input v-model="formData.mobile" placeholder="请输入手机号码" clearable></el-input>
            </el-form-item>
          </el-col>
          <el-col :span='12'>
            <el-form-item label="性别">
              <el-input-number v-model="formData.gender" :min="1" :max="10" label="请输入性别"></el-input-number>
            </el-form-item>
          </el-col>
          <el-col :span='12'>
            <el-form-item label="状态">
              <el-input-number v-model="formData.status" :min="1" :max="10" label="请输入状态"></el-input-number>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button @click="visibleOfDialog = false">取消</el-button>
        <el-button type="primary" :loading="loadingOfSubmit" @click="onSubmit()">保存</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
/**
 * 编辑会员
 */
export default {
  name: "MembersEdit",
  emits: ["submit"],
  data() {
    return {
      visibleOfDialog: false,
      loadingOfSubmit: false,
      id: 0,
      title: "编辑",
      formData: {},
    };
  },
  methods: {
    // 打开对话框（用于外部传参式调用）
    open(title = "编辑", id = 0) {
      this.title = title;
      this.id = id;
      this.visibleOfDialog = true;
    },
    // 加载详情数据
    loadData() {
      console.log('[加载详情数据] 查询参数= ', { params: this.id });
      this.vuiAjax
        .put(this.apiUrls.membersRead, { params: this.id })
        .then((ret) => {
          this.loadingOfSubmit = false;
          console.log("[加载详情数据] 返回结果= ", ret);
          if (!ret.success) {
            this.$message.error(ret.message);
            return;
          }
          this.dataOfForm = ret.data;
        })
        .catch((msgInfo) => {
          this.$message.error(msgInfo.msg);
        });
    },
    // 提交表单数据
    onSubmit() {
      console.log("[提交表单数据] 表单数据= ", this.formData);
      this.loadingOfSubmit = true;
      if (this.id > 0) {
        this.vuiAjax
          .put(this.apiUrls.membersUpdate, { params: this.dataOfForm })
          .then((ret) => {
            console.log("[提交表单数据-更新模式] 返回结果= ", ret);
            this.loadingOfSubmit = false;
            if (!ret.success) {
              this.$message.error(ret.message);
              return;
            }
            this.id = ret.data;
          })
          .catch((msgInfo) => {
            this.loadingOfSubmit = false;
            this.$message.error(msgInfo.msg);
          });
        return;
      }
      this.vuiAjax
        .post(this.apiUrls.membersUpdate, this.dataOfForm)
        .then((ret) => {
          console.log("[提交表单数据-新建模式] 返回结果= ", ret);
          this.loadingOfSubmit = false;
          if (!ret.success) {
            this.$message.error(ret.message);
            return;
          }
          this.id = ret.data;
        })
        .catch((msgInfo) => {
          this.loadingOfSubmit = false;
          this.$message.error(msgInfo.msg);
        });
    },
  },
  created() {
    this.loadData();
  },
};
</script>

<style>
</style>